package com.jsyh.maidu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.ArticleActivity;
import com.jsyh.maidu.activity.SearchActivity;
import com.jsyh.maidu.activity.ShoppingCartActivity;
import com.jsyh.maidu.adapter.CollectionAdapter;
import com.jsyh.maidu.model.Collection;
import com.jsyh.maidu.presenter.CollectionPresenter;
import com.jsyh.maidu.views.CollectionView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/11.
 */

public class CollectionFragment extends BaseFragment implements CollectionView{

    private RecyclerView recyclerView;
    private CollectionPresenter presenter;
    private Collection collectionModel;
    private List<Integer> viewType;
    CollectionAdapter adapter;
    LinearLayoutManager layoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collection, null);
        view.findViewById(R.id.txtMessage).setOnClickListener(this);
        view.findViewById(R.id.lineLaySearch).setOnClickListener(this);
        view.findViewById(R.id.txtGoCar).setOnClickListener(this);
        return view;
    }

    @Override
    protected void initView() {
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.collect_list);
        presenter = new CollectionPresenter(this);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);


    }


    @Override
    protected void initData() {
        super.initData();
        presenter.initCollectData(context);
        viewType = new ArrayList<>();
        for (int i = 0; i < 3; i ++){
            viewType.add(i);
        }
    }

    @Override
    public void getCollectData(Collection model) {
        if (model.getCode().equals("1")){
            collectionModel = model;
            adapter = new CollectionAdapter(viewType, context, collectionModel.getData());
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.txtMessage:
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                startActivity(intent);
                break;
            case R.id.lineLaySearch:
                Intent search = new Intent(getActivity(), SearchActivity.class);
                startActivity(search);
                break;
            case R.id.txtGoCar:
                Intent carIntent =new Intent(getActivity(),ShoppingCartActivity.class);
                startActivity(carIntent);
                break;
        }
    }
}
